<?php

namespace App\Admin\Extensions;

use Encore\Admin\Form\Field;

class AceEditor extends Field
{
    protected $view = 'admin.ace-editor';

    private $mode = 'json';

    protected static $css = [
        '',
    ];

    protected static $js = [
        '/vendor/ace/ace.js',
    ];

    public function mode($name){
        $this->mode = $name;
        return $this;
    }


    public function render()
    {
        $name = $this->formatName($this->column);

        $this->script = <<<EOT
var editor = ace.edit('{$this->id}');
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/{$this->mode}");
var code = editor.getSession().getValue();
var o = JSON.parse(code)
code = JSON.stringify(o, null, 4) 
editor.getSession().setValue(code);
editor.getSession().on('change', function(e) {
$('input[name=$name]').val(editor.getValue());
});
EOT;
        return parent::render();
    }
}